package com.oauth.linkedin.server.util;

import java.util.ArrayList;
import java.util.List;

import com.google.code.linkedinapi.schema.Person;
import com.google.code.linkedinapi.schema.Update;
import com.google.code.linkedinapi.schema.Updates;
import com.rapwaves.feeds.FeedItem;
import com.rapwaves.feeds.FeedOwner;
import com.rapwaves.feeds.Feeds;
import com.rapwaves.feeds.content.FeedContent;
import com.rapwaves.feeds.content.FeedContents;
import com.rapwaves.feeds.content.beans.AbstractContent;
import com.rapwaves.feeds.content.beans.TextContent;

public class LinkedinFeedUtil {

	public static Feeds getUpdates(Updates updates) {
		List<FeedItem> feedItems = new ArrayList<FeedItem>();
		if (updates != null) {
			for (Update update : updates.getUpdateList()) {
				feedItems.add(getFeedItem(update));
			}
		}
		return new Feeds(feedItems);
	}

	public static FeedItem getFeedItem(Update update) {
		FeedOwner owner = getFeedOwner(update.getUpdateContent().getPerson());
		FeedContents contentList = getFeedContents(update);
		FeedItem feed = new FeedItem(owner, contentList, null);
		return feed;
	}

	private static FeedContents getFeedContents(Update update) {
		List<FeedContent> feedContents = new ArrayList<FeedContent>();
		AbstractContent abstractContent = new TextContent(update.getUpdateContent().getPerson().getHeadline()+"\n"+
				update.getUpdateContent().getPerson().getCurrentStatus());
		FeedContent content = new FeedContent(abstractContent.getContentType(),
				abstractContent);
		feedContents.add(content);
		return new FeedContents(feedContents);

	}

	public static FeedOwner getFeedOwner(Person user) {
		if (user != null) {
			String photoUrl = user.getPictureUrl();
			String profileUrl = user.getPublicProfileUrl();
			return new FeedOwner(user.getFirstName()+" "+user.getLastName(), profileUrl, photoUrl);
		}
		return new FeedOwner();
	}


}
